package esi.t2no1.web.message;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import esi.t2no1.dao.message.MessageDAO;
import esi.t2no1.dao.message.MessageDTO;

/**
 * Controller that handles message related views
 * 
 * @author Kaarel Jõgeva
 */
@Controller
@RequestMapping(value = "/message")
public class MessageController {

    private MessageDAO messageDAO;

    @RequestMapping(method = RequestMethod.GET)
    public String getList(Model model) {
        List<MessageDTO> todayMessages = messageDAO.getTodayMessages();
        model.addAttribute("messages", todayMessages);
        return "message/list";

    }

    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public String getView(@PathVariable int id, Model model) {
        MessageDTO message = messageDAO.getMessage(id);
        if (message == null) {
            model.addAttribute("error", "message.notFound");
            return "message/view";
        }
        model.addAttribute("message", message);
        return "message/view";
    }

    @Autowired
    public void setMessageDAO(MessageDAO messageDAO) {
        this.messageDAO = messageDAO;
    }

}
